package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mp.controller.SysConfiguration;
import com.itheima.mp.mapper.SysConfigurationMapper;
import com.itheima.mp.param.*;
import com.itheima.mp.service.SysConfigurationService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;

@Service
public class SysConfigurationServiceImpl extends ServiceImpl<SysConfigurationMapper, SysConfiguration> implements SysConfigurationService {

    @Resource
    private SysConfigurationMapper sysConfigurationMapper;

    @Override
    public SysConfiguration queryEntity(String id) {
        SysConfiguration sysConfiguration = this.getById(id);
        if (ObjectUtil.isEmpty(sysConfiguration)) {
            throw new CommonException("内设数据不存在，id值为：{}", id);
        }
        return sysConfiguration;
    }

    @Override
    public Page<SysConfiguration> page(SysConfigurationPageParam builtDataPageParam) {
        QueryWrapper<SysConfiguration> queryWrapper = new QueryWrapper<>();
        // todo 分页逻辑



        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public SysConfiguration detail(SysConfigurationParam sysConfigurationParam) {
        return this.queryEntity(sysConfigurationParam.getId());
    }

    @Override
    public void delete(CommonValidList<SysConfigurationIdParam> sysConfigurationIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(sysConfigurationIdParamList, SysConfigurationIdParam::getId));
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(SysConfigurationAddParam sysConfigurationAddParam) {
        SysConfiguration sysConfiguration = BeanUtil.toBean(sysConfigurationAddParam, SysConfiguration.class);
        this.save(sysConfiguration);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(SysConfigurationEditParam sysConfigurationEditParam) {
        SysConfiguration sysConfiguration = this.queryEntity(sysConfigurationEditParam.getId());
        BeanUtil.copyProperties(sysConfigurationEditParam, sysConfiguration);
        this.updateById(sysConfiguration);
    }
}
